package com.example.aussiehealthhelper;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.List;

public class HealthNotesActivity extends AppCompatActivity {

    private HealthNotesDbHelper dbHelper;
    private RecyclerView recyclerView;
    private FloatingActionButton fabAddNote;
    private HealthNotesAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_health_notes);

        dbHelper = new HealthNotesDbHelper(this);
        recyclerView = findViewById(R.id.notesRecyclerView);
        fabAddNote = findViewById(R.id.fabAddNote);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        loadNotes();

        fabAddNote.setOnClickListener(v -> {
            Intent intent = new Intent(HealthNotesActivity.this, HealthNoteEditActivity.class);
            startActivity(intent);
        });
    }

    private void loadNotes() {
        List<HealthNote> notes = dbHelper.getAllNotes();
        adapter = new HealthNotesAdapter(notes, note -> {
            //编辑笔记
            Intent intent = new Intent(HealthNotesActivity.this, HealthNoteEditActivity.class);
            intent.putExtra("note_id", note.getId());
            startActivity(intent);
        });
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //每次返回时刷新数据
        loadNotes();
    }
}